package admin

import (
	"object/go_cms/models"
)

type ConfigAdminController struct {
	BaseAdminController
	models models.ConfigModels
}

// @router /admin/config/indexUI [get]
func (c *ConfigAdminController) IndexUI() {
	c.TplAdmin("config/index")
}

// @router /admin/config/get [get,post]
func (c *ConfigAdminController) Get() {
	c.Res.Code = 1
	id := c.GetString("name")
	con, err := c.models.Get(id)
	if err != nil {
		c.Res.Code = 0
	} else {
		c.Res.Data = con
	}
	c.Json(c.Res)
}

// @router /admin/config/query [get,post]
func (c *ConfigAdminController) Query() {
	c.Res.Code = 0
	maps := c.GetParamMap()
	c.Res.Data, _ = c.models.Query(maps)
	c.Json(c.Res)
}

// @router /admin/config/update [get,post]
func (c *ConfigAdminController) Update() {
	c.Res.Code = 1
	c.Res.Msg = "保存成功"
	var kv map[string]interface{}
	var row int64
	var err error
	maps := c.GetParamMap()
	for key, val := range maps {
		if val != nil {
			kv = make(map[string]interface{})
			kv["name"] = key
			kv["value"] = val
			row, err = c.models.Update(kv)
		}
	}
	if err != nil {
		c.Res.Code = 0
		c.Res.Msg = err.Error()
	} else {
		c.Res.Data = row
	}
	c.Json(c.Res)
}

// @router /admin/config/sendMail [get,post]
func (c *ConfigAdminController) Send() {
	user, _ := c.models.Get("smtp_user")
	password, _ := c.models.Get("smtp_pass")
	host, _ := c.models.Get("smtp_sver")
	port, _ := c.models.Get("smtp_port")
	subject, _ := c.models.Get("smtp_name")
	to := c.GetString("user")
	body := c.GetString("cent")
	mailtype := "html"

	bo := c.SendMail(user.Value, password.Value, host.Value+":"+port.Value, to, subject.Value, body, mailtype)
	if !bo {
		c.Res.Code = 0
		c.Res.Msg = "发送失败"
	}
	c.Res.Code = 1
	c.Res.Msg = "发送成功"
	c.Json(c.Res)
}
